﻿#include "FITKRadiossAdaptorTimerHistory.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossSolution.h"
#include "FITK_Component/FITKRadiossData/FITKAbstractProbe.h"
#include "FITK_Component/FITKRadiossData/FITKTimeHistoryACCEL.h"
#include "FITK_Component/FITKRadiossData/FITKTimeHistoryINTER.h"
#include "FITK_Component/FITKRadiossData/FITKTimeHistoryRBODY.h"
#include "FITK_Component/FITKRadiossData/FITKTimeHistoryRWALL.h"
#include "FITK_Component/FITKRadiossData/FITKTimeHistorySECTIO.h"

#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"

#include "FITKRadiossRadReader.h"
#include "FITKRadiossRadWriter.h"
#include "RadKeyLineSpliter.h"
#include "RadReadInformationMapper.h"
#include "RadWriteInformationMapper.h"

namespace Radioss
{
    QString FITKRadiossAdaptorTimerHistory::getAdaptorClass()
    {
        return "Radioss::FITKAbstractTimeHistory";
    }

    bool FITKRadiossAdaptorTimerHistory::adaptR()
    {
        if (_caseData == nullptr || _reader == nullptr) return false;
        RadKeyLineSpliter* spliter = _reader->getKeyLineSpliter();
        RadReadInformationMapper* mapper = _reader->getReadRadInformationMapper();
        if (!mapper || !spliter)return false;
        FITKRadiossSolution* solution = _caseData->getCurrentSolution();
        if (!solution)return false;
        FITKTimeHistoryManager* thMgr = solution->getTimeHietoryManager();
        if (!thMgr)return false;

        //读取rad ID和名称
        const int radID = spliter->getId(0);
        if (radID <= 0) return false;
        const QString name = _reader->readLine();
        if (name.isEmpty()) return false;

        //判断类型
        QString strType = spliter->getKeys();
        FITKAbstractTimeHistory* th{};
        if (strType == "/TH/ACCEL")
            th = new FITKTimeHistoryACCEL;
        else if (strType == "/TH/INTER")
            th = new FITKTimeHistoryINTER;
        else if (strType == "/TH/RBODY")
            th = new FITKTimeHistoryRBODY;
        else if (strType == "/TH/RWALL")
            th = new FITKTimeHistoryRWALL;
        else if (strType == "/TH/SECTIO")
            th = new FITKTimeHistorySECTIO;

         //参数存储
        QStringList lines;
        while (!_reader->atEnd())
        {
            QString line = _reader->readLine();
            if (line.startsWith("#")) continue;
            if (line.startsWith("/"))
            {
                _reader->backLine();
                break;
            }
            lines.append(line);
        }
        //与手册说明不符
        if (lines.size() != 2)
        {
            _reader->consoleMessage(3, QString("Invalid /TH : %1.").arg(name));
            return false;
        }
        //读取赋予参数
        if (!this->readParam(lines, th)) {
            delete th;
            return false;
        }
        th->setDataObjectName(name);
        thMgr->appendDataObj(th);

        return true;
    }

    bool FITKRadiossAdaptorTimerHistory::adaptW()
    {
        FITKAbstractTimeHistory*  th = dynamic_cast<FITKAbstractTimeHistory*>(_dataObj);
        if (th == nullptr || _writer == nullptr) return false;

        QStringList variableKeyWords = th->getVariableKeyWords();
        QList<int> objIDs = th->getObjIDs();
        if (variableKeyWords.isEmpty()|| objIDs.isEmpty())return false;

        QTextStream* stream = _writer->getStream();
        RadWriteInformationMapper* infoMapper = _writer->getWriteInpInformationMapper();
        if (stream == nullptr || infoMapper == nullptr) return false;
        //写出关键字行  ID 从1开始递增
        int radID = infoMapper->getMaxRadTimeHID() + 1;
        //建立映射关系
        infoMapper->insertTimeHIDMap(th->getDataObjectID(), radID);
        QString keyWords{};
        FITKAbstractTimeHistory::TimeHistoryType thType = th->getTimeHistoryType();
        if (thType == FITKAbstractTimeHistory::TimeHistory_ACCEL)
            keyWords = "/TH/ACCEL/";
        else if (thType == FITKAbstractTimeHistory::TimeHistory_INTER) 
            keyWords = "/TH/INTER/";
        else if (thType == FITKAbstractTimeHistory::TimeHistory_RWALL) 
            keyWords = "/TH/RWALL/";
        else if (thType == FITKAbstractTimeHistory::TimeHistory_RBODY) 
            keyWords = "/TH/RBODY/";
        else if (thType == FITKAbstractTimeHistory::TimeHistory_SECTIO) 
            keyWords = "/TH/SECTIO/";

        *stream << keyWords << radID << endl;

        //写名称
        *stream << th->getDataObjectName() << endl;

        //写出变量组
        *stream << "#     var1";
        for (int i = 2; i <= 10; ++i)
            *stream << QString("      var%1").arg(i);
        *stream << endl;
        for (QString keyWords : variableKeyWords)
            *stream << keyWords.leftJustified(10, ' ');
        *stream << endl;

        //写出对象ID
        *stream << "#     Obj1";
        for (int i = 2; i <= 10; ++i)
            *stream << QString("      Obj%1").arg(i);
        *stream << endl;
        if (thType == FITKAbstractTimeHistory::TimeHistory_ACCEL) {
            for (int objID : objIDs)
                *stream << gapStr(infoMapper->getRadIDByAccelID(objID));
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistory_INTER) {
            for (int objID : objIDs)
                *stream << gapStr(infoMapper->getRadIDByInteractionID(objID));
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistory_RWALL) {
            for (int objID : objIDs)
                *stream << gapStr(infoMapper->getRadIDByRWallID(objID));
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistory_RBODY) {
            for (int objID : objIDs)
                *stream << gapStr(infoMapper->getRadIDByConnectionID(objID));
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistory_SECTIO) {
            //to do
        }

        *stream << endl;

        return true;
    }

    bool FITKRadiossAdaptorTimerHistory::readParam(const QStringList & lines, FITKAbstractTimeHistory * th)
    {
        if(lines.size()!=2||!th)return false;
        QStringList keyWords = lines[0].simplified().split(" ");
        QStringList objStrIDs = lines[1].simplified().split(" ");
        QList<int> objIDs{};
        for (QString objID: objStrIDs) {
            bool ok = false;
            int id = objID.toInt(&ok);
            if (ok)objIDs.append(id);
        }
        if (keyWords.isEmpty() || objIDs.isEmpty())return false;

        FITKAbstractTimeHistory::TimeHistoryType thType = th->getTimeHistoryType();
        if (thType == FITKAbstractTimeHistory::TimeHistoryType::TimeHistory_ACCEL){
            FITKTimeHistoryACCEL* tH = dynamic_cast<FITKTimeHistoryACCEL*>(th);
            if (!tH) return false;
            //变量组类型
            if (keyWords.contains("DEF"))
                tH->setVarlableGroupType(FITKTimeHistoryACCEL::THACCELVariableGroup::VGACCEL_DEF);
            else if (keyWords.contains("W"))
                tH->setVarlableGroupType(FITKTimeHistoryACCEL::THACCELVariableGroup::VGACCEL_W);
            //其余变量关键字
            QList<FITKTimeHistoryACCEL::TimeHistoryACCELVariable> variables;
            // 枚举转换字符 
            Core::FITKEnumTransfer<FITKTimeHistoryACCEL::TimeHistoryACCELVariable> fitkEleTypeTrafer;
            bool isValid = false;
            for (QString keyWord : keyWords) {
                auto keyType = fitkEleTypeTrafer.fromString(keyWord, isValid);
                if (!isValid) continue;
                variables.append(keyType);
            }
            tH->setCustomizeVariable(variables);
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistoryType::TimeHistory_INTER){
            FITKTimeHistoryINTER* tH = dynamic_cast<FITKTimeHistoryINTER*>(th);
            if (!tH) return false;
            //变量组类型
            if (keyWords.contains("DEF"))
                tH->setVarlableGroupType(FITKTimeHistoryINTER::THINTERVariableGroup::VGINTER_DEF);
            else if (keyWords.contains("FN"))
                tH->setVarlableGroupType(FITKTimeHistoryINTER::THINTERVariableGroup::VGINTER_FN);
            else if (keyWords.contains("FT"))
                tH->setVarlableGroupType(FITKTimeHistoryINTER::THINTERVariableGroup::VGINTER_FT);
            else if (keyWords.contains("|FN|"))
                tH->setVarlableGroupType(FITKTimeHistoryINTER::THINTERVariableGroup::VGINTER_AbsoluteFN);
            else if (keyWords.contains("|FT|"))
                tH->setVarlableGroupType(FITKTimeHistoryINTER::THINTERVariableGroup::VGINTER_AbsoluteF);
            //其余变量关键字
            QList<FITKTimeHistoryINTER::TimeHistoryINTERVariable> variables;
            QString keyWord = lines[0];
            keyWord.replace("|", "_");// TH/INTER有绝对值符号,用下划线代替
            QStringList keyWords= keyWord.simplified().split(" ");
            // 枚举转换字符 
            Core::FITKEnumTransfer<FITKTimeHistoryINTER::TimeHistoryINTERVariable> fitkEleTypeTrafer;
            bool isValid = false;
            for (QString keyWord : keyWords) {
                auto keyType = fitkEleTypeTrafer.fromString(keyWord, isValid);
                if (!isValid) continue;
                variables.append(keyType);
            }
            tH->setCustomizeVariable(variables);
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistoryType::TimeHistory_RWALL){
            FITKTimeHistoryRWALL* tH = dynamic_cast<FITKTimeHistoryRWALL*>(th);
            if (!tH) return false;
            //变量组类型
            if (keyWords.contains("DEF"))
                tH->setVarlableGroupType(FITKTimeHistoryRWALL::THRWALLVariableGroup::VGRWALL_DEF);
            else if (keyWords.contains("FN"))
                tH->setVarlableGroupType(FITKTimeHistoryRWALL::THRWALLVariableGroup::VGRWALL_FN);
            else if (keyWords.contains("FT"))
                tH->setVarlableGroupType(FITKTimeHistoryRWALL::THRWALLVariableGroup::VGRWALL_FT);
            //其余变量关键字
            QList<FITKTimeHistoryRWALL::TimeHistoryRWALLVariable> variables;
            // 枚举转换字符 
            Core::FITKEnumTransfer<FITKTimeHistoryRWALL::TimeHistoryRWALLVariable> fitkEleTypeTrafer;
            bool isValid = false;
            for (QString keyWord : keyWords) {
                auto keyType = fitkEleTypeTrafer.fromString(keyWord, isValid);
                if (!isValid) continue;
                variables.append(keyType);
            }
            tH->setCustomizeVariable(variables);
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistoryType::TimeHistory_RBODY){
            FITKTimeHistoryRBODY* tH = dynamic_cast<FITKTimeHistoryRBODY*>(th);
            if (!tH) return false;
            //变量组类型
            if (keyWords.contains("DEF"))
                tH->setVarlableGroupType(FITKTimeHistoryRBODY::THRBODYVariableGroup::VGRBODY_DEF);
            else if (keyWords.contains("F"))
                tH->setVarlableGroupType(FITKTimeHistoryRBODY::THRBODYVariableGroup::VGRBODY_F);
            else if (keyWords.contains("M"))
                tH->setVarlableGroupType(FITKTimeHistoryRBODY::THRBODYVariableGroup::VGRBODY_M);
            else if (keyWords.contains("R"))
                tH->setVarlableGroupType(FITKTimeHistoryRBODY::THRBODYVariableGroup::VGRBODY_R);
            else if (keyWords.contains("FI"))
                tH->setVarlableGroupType(FITKTimeHistoryRBODY::THRBODYVariableGroup::VGRBODY_FI);
            else if (keyWords.contains("MI"))
                tH->setVarlableGroupType(FITKTimeHistoryRBODY::THRBODYVariableGroup::VGRBODY_MI);
            //其余变量关键字
            QList<FITKTimeHistoryRBODY::TimeHistoryRBODYVariable> variables;
            // 枚举转换字符 
            Core::FITKEnumTransfer<FITKTimeHistoryRBODY::TimeHistoryRBODYVariable> fitkEleTypeTrafer;
            bool isValid = false;
            for (QString keyWord : keyWords) {
                auto keyType = fitkEleTypeTrafer.fromString(keyWord, isValid);
                if (!isValid) continue;
                variables.append(keyType);
            }
            tH->setCustomizeVariable(variables);
        }
        else if (thType == FITKAbstractTimeHistory::TimeHistoryType::TimeHistory_SECTIO){
            FITKTimeHistorySECTIO* tH = dynamic_cast<FITKTimeHistorySECTIO*>(th);
            if (!tH) return false;
            //变量组类型
            if (keyWords.contains("DEF"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_DEF);
            else if (keyWords.contains("FN"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_FN);
            else if (keyWords.contains("FT"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_FT);
            else if (keyWords.contains("M"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_M);
            else if (keyWords.contains("CENTER"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_CENTER);
            else if (keyWords.contains("GLOBAL"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_GLOBAL);
            else if (keyWords.contains("LOCAL"))
                tH->setVarlableGroupType(FITKTimeHistorySECTIO::THSECTIOVariableGroup::VGSECTIO_LOCAL);
            //其余变量关键字
            QList<FITKTimeHistorySECTIO::TimeHistorySECTIOVariable> variables;
            // 枚举转换字符 
            Core::FITKEnumTransfer<FITKTimeHistorySECTIO::TimeHistorySECTIOVariable> fitkEleTypeTrafer;
            bool isValid = false;
            for (QString keyWord : keyWords) {
                auto keyType = fitkEleTypeTrafer.fromString(keyWord, isValid);
                if (!isValid) continue;
                variables.append(keyType);
            }
            tH->setCustomizeVariable(variables);
        }
        else
            return false;
        th->setObjIDs(objIDs);
        return true;
    }


}